from django.contrib import admin
from .models import VipBagCashAllot
from .models import LevelDesc
from .models import BuyUserLeaderIncomeAllot
from .models import LeaderLowerIncomeAllot
from .models import Menus
from .models import PublicAccount
from django.contrib.auth import get_user_model

User = get_user_model()


class UserInfo(admin.ModelAdmin):
    list_display = ("id", "name", "mobile", "flag", "coffee_new_user", "lower_num", "level_desc", "create_time")
    exclude = ("user_permissions",)

    def level_desc(self, model):
        if model.level:
            return model.level.get_level_display()
        else:
            return "无等级"

    level_desc.short_description = "级别"
    search_fields = ("mobile",)


class BuyUserLeaderIncomeAllotAdmin(admin.ModelAdmin):
    list_display = ("id", "buy_user_level_desc", "income_user_level_desc", "allot")

    def buy_user_level_desc(self, model):
        return model.buy_user_level.get_level_display()

    def income_user_level_desc(self, model):
        return model.income_user_level.get_level_display()

    buy_user_level_desc.short_description = "购买人等级"
    income_user_level_desc.short_description = "推荐人等级"


class LeaderLowerIncomeAllotAdmin(admin.ModelAdmin):
    list_display = ("id", "lower_user_level_desc", "leader_user_level_desc", "allot")

    def lower_user_level_desc(self, model):
        return model.lower_user_level.get_level_display()

    def leader_user_level_desc(self, model):
        return model.leader_user_level.get_level_display()

    lower_user_level_desc.short_description = "推荐人等级"
    leader_user_level_desc.short_description = "推荐人上级等级"


class CashAllotAdmin(admin.ModelAdmin):
    list_display = ("id", "level_desc", "num", "cash_num", "leader_desc")

    def leader_desc(self, model):
        if model.leader:
            return model.leader.get_level_display()
        else:
            return "最高级别"

    def level_desc(self, model):
        return model.get_level_display()

    leader_desc.short_description = "上级"
    level_desc.short_description = "级别"


class VipBagCashAllotAdmin(admin.ModelAdmin):
    list_display = ("id", "level_name", "income")

    def level_name(self, model):
        return model.level.get_level_display()

    level_name.short_description = "受益人级别"


class MenusAdmin(admin.ModelAdmin):
    list_display = ("id", "name", "show_name")

    def show_name(self, model):
        if model.parents:
            return model.parents.name
        else:
            return model.name

    show_name.short_description = "展示的菜单"


class PublicAccountAdmin(admin.ModelAdmin):
    list_display = ("id", "company", "sign")


admin.site.register(LevelDesc, CashAllotAdmin)
admin.site.register(PublicAccount, PublicAccountAdmin)
admin.site.register(VipBagCashAllot, VipBagCashAllotAdmin)
admin.site.register(BuyUserLeaderIncomeAllot, BuyUserLeaderIncomeAllotAdmin)
admin.site.register(LeaderLowerIncomeAllot, LeaderLowerIncomeAllotAdmin)
admin.site.register(User, UserInfo)
admin.site.register(Menus, MenusAdmin)
